import Vue from 'vue'
import Vuex from 'vuex'

import avatar from '@/assets/images/user_default.jpg'
import http from '@/api/http'
import createPersistedstate from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    avatar: avatar,
    token: '',
    addressList: [
      // {
      //   id: '1',
      //   name: '张三',
      //   tel: '13000000000',
      //   address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
      //   province: '浙江省',
      //   city: '杭州市',
      //   county: '西湖区',
      //   areaCode: '330106',
      //   addressDetail: '文三路 138 号东方通信大厦 7 楼 501 室',
      //   postalCode: '200122',
      //   isDefault: true
      // },
      // {
      //   id: '2',
      //   name: '李四',
      //   tel: '13100000000',
      //   address: '浙江省杭州市拱墅区莫干山路 50 号',
      //   province: '浙江省',
      //   city: '杭州市',
      //   county: '西湖区',
      //   areaCode: '330105',
      //   addressDetail: '拱墅区莫干山路 50 号',
      //   postalCode: '200122'
      // }
    ]
  },
  mutations: {
    setList (state, payload) {
      state.addressList = payload.map(item => {
        return {
          id: item.id,
          name: item.receiver,
          tel: item.contact,
          address: item.address,
          province: '',
          city: '',
          county: '',
          areaCode: item.countyCode,
          addressDetail: item.address,
          postalCode: item.postalCode
        }
      })
    },
    addAddress (state, payload) {
      if (payload.isDefault) {
        state.addressList.forEach(item => {
          item.isDefault = false
        })
      }
      state.addressList.unshift(payload)
    },
    delAddress (state, payload) {
      const index = state.addressList.findIndex(item => item.id === payload.id)
      state.addressList.splice(index, 1)
    },
    editAddress (state, payload) {
      if (payload.isDefault) {
        state.addressList.forEach(item => {
          item.isDefault = false
        })
      }
      const item = state.addressList.find(item => item.id === payload.id)
      for (const key in payload) {
        item[key] = payload[key]
      }
    },
    setAvatar (state, payload) {
      state.avatar = payload
    },
    setToken (state, token) {
      state.token = token
    }
  },
  actions: {
    findAddress (ctx) {
      http.get('/member/address').then(res => {
        ctx.commit('setList', res.data.result)
      })
    },
    addAddress (ctx, payload) {
      return new Promise((resolve, reject) => {
        // window.setTimeout(() => {
        //   ctx.commit('addAddress', payload)
        //   resolve({ msg: '添加成功', result: '' })
        // }, 500)
        const address = {
          receiver: payload.name,
          contact: payload.tel,
          province: payload.province,
          city: payload.city,
          county: payload.area,
          provinceCode: payload.areaCode.substr(0, 2) + '0000',
          cityCode: payload.areaCode.substr(0, 4) + '00',
          countyCode: payload.areaCode,
          address: payload.addressDetail,
          fullLocation: '',
          addressTags: '',
          postalCode: payload.postalCode,
          isDefault: 0
        }
        http.post('/member/address', address).then(res => {
          ctx.commit('addAddress', res.data.result)
          resolve({ msg: '添加成功', result: '' })
        })
      })
    },
    delAddress (ctx, payload) {
      return new Promise((resolve, reject) => {
        // window.setTimeout(() => {
        //   ctx.commit('delAddress', payload)
        //   resolve({ msg: '删除成功', result: '' })
        // }, 500)
        http.delete('/member/address/' + payload.id).then(res => {
          // ctx.commit('addAddress', res.data.result)
          resolve({ msg: '删除成功', result: '' })
        })
      })
    },
    editAddress (ctx, payload) {
      return new Promise((resolve, reject) => {
        // window.setTimeout(() => {
        //   ctx.commit('editAddress', payload)
        //   resolve({ msg: '编辑成功', result: '' })
        // }, 500)
        const address = {
          receiver: payload.name,
          contact: payload.tel,
          province: payload.province,
          city: payload.city,
          county: payload.area,
          provinceCode: payload.areaCode.substr(0, 2) + '0000',
          cityCode: payload.areaCode.substr(0, 4) + '00',
          countyCode: payload.areaCode,
          address: payload.addressDetail,
          fullLocation: '',
          addressTags: '',
          postalCode: payload.postalCode,
          isDefault: 0
        }
        http.put('/member/address/' + payload.id, address).then(res => {
          // ctx.commit('editAddress', res.data.result)
          resolve({ msg: '编辑成功', result: '' })
        })
      })
    }
  },
  modules: {
  },
  plugins: [
    createPersistedstate({
      key: 'erabbit-client-hybrid'
    })
  ]
})
